//****************************************************************
//
//  初始化树函数
//
//****************************************************************
function initTree(data) {
    var setting = {
        callback: {
            onClick: zTreeOnClick

        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: "0"
            },
            key: {
                //title: "title"
            }
        },
        edit: {
            enable: false
        },
        view: {
            selectedMulti: false
        }
    };

    $.fn.zTree.init($("#tree"), setting, data.returnObject);

    var treeObj = $.fn.zTree.getZTreeObj("tree");
    treeObj.expandAll(false);
    var nodes = treeObj.getNodes();
    if (nodes.length > 0) {
        treeObj.selectNode(nodes[0], true);
        treeObj.expandNode(nodes[0], true, false, false);
        zTreeOnClick(null, "tree", nodes[0]);
    }
}

/**
 * 层叠展示 不需要初始化显示的
 * @param data
 */
function singleInitTree(data) {
    var setting = {
        callback: {
            onClick: zTreeOnClick,
            onExpand:zTreeOnExpand

        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: "0"
            },
            key: {
                //title: "title"
            }
        },
        edit: {
            enable: false
        },
        view: {
            selectedMulti: false
        }
    };

    $.fn.zTree.init($("#tree"), setting, data.returnObject);
}

/**
 * 层叠展示 需要初始化显示的
 * @param data
 */
function singleInitTreeExtend(data) {
    var setting = {
        callback: {
            onClick: zTreeOnClick,
            onExpand:zTreeOnExpand

        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: "0"
            },
            key: {
                //title: "title"
            }
        },
        edit: {
            enable: false
        },
        view: {
            selectedMulti: false
        }
    };

    $.fn.zTree.init($("#tree"), setting, data.returnObject);
    //默认点击当前机构树
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    treeObj.expandAll(false);
    var nodes = treeObj.getNodes();
    if (nodes.length > 0) {
        treeObj.selectNode(nodes[0], true);
        treeObj.expandNode(nodes[0], true, false, false);
        zTreeOnClick(null, "tree", nodes[0]);
    }
}

function refTreeOnClick(event, treeName, treeNode) {
    selectedNode = treeNode;
    btnQuery();
};

function ajaxData(actionUrl, args, name, setting) {
    $.ajax({
        cache: false,
        url: actionUrl,
        type: "post",
        dataType: "json",
        data: args,
        async: true,
        beforeSend: function (XMLHttpRequest) {

        },
        success: function (data, textStatus) {
            var analyMsg = data;

            if (analyMsg.code == "0000") {
                initRefTree(name, data, setting);
                return;
            }
            else if (analyMsg.code == "9999") {
                //$.messager.alert("友情提醒", analyMsg.msg, "info", function () {
                $.messager.alert("信息", analyMsg.msg, "info", function () {
                    var top = window;
                    while (top != top.parent) {
                        top = top.parent;
                    } //获取当前页面的顶层窗口对象
                    top.location.href = "/system/register/toLogin"; //跳转到登陆页面
                });
                return;
            } else {
                $.messager.alert("友情提醒", analyMsg.msg, "info", function () {
                });
                return;
            }
        },
        error: function (data, textStatus, errorThrown) {
            $.messager.alert('操作提示', data.responseText, "info", function () {

                    if (data.responseText == '系统超时，请重新登录。') {
                        var top = window;
                        while (top != top.parent) {
                            top = top.parent;
                        } //获取当前页面的顶层窗口对象
                        top.location.href = "/system/register/toLogin"; //跳转到登陆页面
                    }

            });

        },
        complete: function (XMLHttpRequest, textStatus) {

        }
    });

}


function ajaxDataFilter(treeId, parentNode, responseData) {
    var data;
    if (responseData) {
        data = responseData.returnObject
    }
    return data;
};

function initRefTree(treeName, data, setting) {

    $.fn.zTree.init($("#" + treeName), setting, data.returnObject);
    var treeObj = $.fn.zTree.getZTreeObj(treeName);
    treeObj.expandAll(false);
    var nodes = treeObj.getNodes();
    if (nodes.length > 0) {
        treeObj.selectNode(nodes[0], true);
        treeObj.expandNode(nodes[0], true, false, false);
        selectedNode = nodes[0];
        //treeObj.setting.callback.onClick=(null, treeName, nodes[0]);
        //refTreeOnClick(null, treeName, nodes[0]);
    }
}